/*
 * 描述
 * Created by: litangyu
 * 包含类名列表
 * 版本信息，版本号 v1.0
 * 创建日期 16/9/5
 * 版权声明
*/
package com.foton.logistic.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.foton.logistic.R;
import com.foton.logistic.util.image.fresco.FrescoUtils;
import com.foton.logistic.util.image.fresco.InstrumentedDraweeView;

import java.util.List;

/**
 * 描述
 * Created by: litangyu<br/>
 * Created on: 16/9/5 下午2:21
 */
public class UploadImageAdapter extends BaseAdapter {
    private List<String> data;
    private int MAX_COUNT;
    private final Context mContext;
    private boolean isShowType;

    /**
     * @param list       数据集合
     * @param maxCount   最大显示数量
     * @param isShowType true 显示图片  false 上传图片
     */
    public UploadImageAdapter(Context context, List<String> list, int maxCount,
                              boolean isShowType) {
        this.mContext = context;
        this.data = list;
        this.MAX_COUNT = maxCount;
        this.isShowType = isShowType;
    }

    @Override
    public int getCount() {
        int size;
        if (!isShowType) {
            size = data.size();
            if (size < MAX_COUNT) {
                return size + 1;
            } else {
                return size;
            }
        } else {
            return data.size();
        }
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.base_adapter_upload_image, null);
            holder = new ViewHolder();

            holder.mRv_add = (RelativeLayout) convertView.findViewById(R.id.rl_add);
            holder.mImv_photo = (InstrumentedDraweeView) convertView.findViewById(R.id.imv);
//            holder.mImv_add = (ImageView) convertView.findViewById(R.id.imv_add);
//            holder.mTv_add = (TextView) convertView.findViewById(R.id.tv_add);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (!isShowType) {
            if (position == data.size()) {
                holder.mRv_add.setVisibility(View.VISIBLE);
                holder.mImv_photo.setVisibility(View.GONE);
            } else {
                holder.mRv_add.setVisibility(View.GONE);
                holder.mImv_photo.setVisibility(View.VISIBLE);
                FrescoUtils.displayImage(holder.mImv_photo, "file://" + data.get(position), 120,
                        120);
            }
        } else {
            FrescoUtils.displayImage(holder.mImv_photo, data.get(position));
        }
        return convertView;
    }

    class ViewHolder {
        //        private TextView mTv_add;
//        private ImageView mImv_add;
        private InstrumentedDraweeView mImv_photo;
        private RelativeLayout mRv_add;
    }
}
